﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using Storage.RemoteStorage.Sevice.Entity;
using System.IO;

namespace StorageManager.Interface
{
    public class CloudStorageHelper
    {
        public string RootServiceURL { get; set; }
        public RemoteStorageClient DiscoverService = null;
        public TransferServiceClient TransfertService = null;

        public CloudStorageHelper(string serviceURL)
        {
            RootServiceURL = serviceURL;
            InitDiscover(RootServiceURL);
            InitTransfer(RootServiceURL);
        }

        private void InitTransfer(string serviceURL)
        {

            BasicHttpBinding bindingTransfert = new BasicHttpBinding();
            bindingTransfert.SendTimeout = TimeSpan.FromMinutes(1);
            bindingTransfert.OpenTimeout = TimeSpan.FromMinutes(1);
            bindingTransfert.CloseTimeout = TimeSpan.FromMinutes(1);
            bindingTransfert.ReceiveTimeout = TimeSpan.FromMinutes(10);
            bindingTransfert.MaxReceivedMessageSize = 2147483647;
            bindingTransfert.AllowCookies = false;
            bindingTransfert.BypassProxyOnLocal = false;
            bindingTransfert.HostNameComparisonMode = HostNameComparisonMode.StrongWildcard;
            bindingTransfert.MessageEncoding = WSMessageEncoding.Mtom;
            bindingTransfert.TextEncoding = System.Text.Encoding.UTF8;
            bindingTransfert.TransferMode = TransferMode.Streamed;
            bindingTransfert.UseDefaultWebProxy = true;
            TransfertService = new TransferServiceClient(bindingTransfert, new EndpointAddress(serviceURL + "RemoteStorage.svc"));

        }
        private void InitDiscover(string serviceURL)
        {
            BasicHttpBinding bindingDiscover = new BasicHttpBinding();
            bindingDiscover.SendTimeout = TimeSpan.FromMinutes(1);
            bindingDiscover.OpenTimeout = TimeSpan.FromMinutes(1);
            bindingDiscover.CloseTimeout = TimeSpan.FromMinutes(1);
            bindingDiscover.ReceiveTimeout = TimeSpan.FromMinutes(10);
            bindingDiscover.MaxReceivedMessageSize = 2147483647;
            bindingDiscover.AllowCookies = false;
            bindingDiscover.BypassProxyOnLocal = false;
            bindingDiscover.HostNameComparisonMode = HostNameComparisonMode.StrongWildcard;
            bindingDiscover.MessageEncoding = WSMessageEncoding.Text;
            bindingDiscover.TextEncoding = System.Text.Encoding.UTF8;
            bindingDiscover.TransferMode = TransferMode.Buffered;
            bindingDiscover.UseDefaultWebProxy = true;
            DiscoverService = new RemoteStorageClient(bindingDiscover, new EndpointAddress(serviceURL + "TransferService.svc"));
        }

        #region Directory Action
        public VirtualFolder GetFolder(string Name, VirtualFolder Parent = null)
        {
            if (DiscoverService == null)
                InitDiscover(RootServiceURL);
            return DiscoverService.GetFolder(Name, Parent);
        }
        public VirtualFolder CreateFolder(string Name, VirtualFolder Parent = null)
        {
            if (DiscoverService == null)
                InitDiscover(RootServiceURL);
            return DiscoverService.CreateFolder(Name, Parent);
        }
        public bool DeleteFolder(string Name, VirtualFolder Parent = null)
        {
            if (DiscoverService == null)
                InitDiscover(RootServiceURL);
            return DiscoverService.DeleteFolder(Name, Parent);
        }
        public bool MoveFolder(string Name, VirtualFolder newParent, VirtualFolder Parent = null)
        {
            if (DiscoverService == null)
                InitDiscover(RootServiceURL);
            return DiscoverService.MoveFolder(Name, Parent, newParent);
        }
        #endregion

        #region Upload/Download

        public void UploadLocalFile(FileInfo LocalFile, VirtualFolder Parent, ProgressStreamReportEventArgs Progresss)
        {
            if (!LocalFile.Exists)
                throw new Exception("Fichier Local introuvable");
            if (DiscoverService == null)
                InitDiscover(RootServiceURL);
            if (TransfertService == null)
                InitTransfer(RootServiceURL);
            try
            {
                TransfertService.UploadFile(
                            Path.Combine(Parent.RelativePath, LocalFile.Name),
                            LocalFile.Length, LocalFile.OpenRead());
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #endregion
    }
}
